﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：StaffManage.ascx
//
//文件功能描述：员工管理
// 
//创建标识：2007.11.21 曹骞
//创建描述：
//
//修改标识: 2007.11.28 曹骞 
//修改描述：对模板列的数据绑定方法进行了修改
//
------------------------------------*/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using localhost; 
/// <summary>
/// 员工管理
/// </summary>
public partial class UserControls_StaffManage : System.Web.UI.UserControl
{  
    //定义一个静态的dataset以备后用
    private static DataSet ds = new DataSet();

    /// <summary>
    /// 初始化绑定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //绑定初始化员工列表
            bind();
 
        }

        
    }

/// <summary>
/// 绑定初始girdview数据源
/// </summary>
    void bind()
    {
        StaffService Service = new StaffService();
        //获取所有员工列表
        ds = Service.GetList();
        this.gdvUser.DataSource = ds;
        gdvUser.DataBind();
        this.GridviewBind(ds);
        this.lblAcount.Text = string.Empty;
        this.lblStaffID.Text = string.Empty;
       
    }

    /// <summary>
    /// 触发员工管理事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvUser_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //获取当前行下标
        int iIndex = Convert.ToInt32(e .CommandArgument);
        //获取当前行主键

        this.lblAcount.Text = this.gdvUser.DataKeys[iIndex].Values["Account"].ToString();
        this.lblStaffID .Text=this.gdvUser.DataKeys [iIndex].Values["StaffID"].ToString ();

        //传递参数给转向页面
        if (e.CommandName == "manage")
        {
            Server.Transfer("StaffAdd.aspx");
        }
 
    }
   /// <summary>
   /// 条件查询员工列表
   /// </summary>
   /// <param name="sender"></param>
   /// <param name="e"></param>
    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        //实例化一个webservice的员工管理实例
        StaffService ServiceStaff=new StaffService ();
        
      
        try
        {   //异步调用取得员工列表的方法
            ServiceStaff.GetListByAccountCompleted += new GetListByAccountCompletedEventHandler(Service_GetListByAccountCompleted);
            ServiceStaff.GetListByAccountAsync(this.txtAccount.Text);     
            GridviewBind(ds);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message );
        }
    }
    /// <summary>
    /// 取得回调结果
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void Service_GetListByAccountCompleted(object sender, GetListByAccountCompletedEventArgs e)
    {
        //绑定数据源，显示查询结果
        ds = e.Result;
        GridviewBind(ds);  
    }

    /// <summary>
    ///  分页显示 
    /// </summary>
    /// <param name="ds"></param>
    private void GridviewBind(DataSet ds)
    {
        //初始绑定时显示翻页超链
        btnFirst.Visible = true;
        btnPrev.Visible = true;
        btnNext.Visible = true;
        btnLast.Visible = true;

        LblCurrentIndex.Visible = true;
        LblPageCount.Visible = true;
        LblRecordCount.Visible = true;
        LblNoRecord.Visible = false;

        try
        {
            //绑定数据源
            this.gdvUser.DataSource = ds.Tables[0].DefaultView;
            this.gdvUser.DataBind();

            //显示分页提示
            LblCurrentIndex.Text = "第 " + (this.gdvUser.PageIndex + 1).ToString() + " 页";
            LblPageCount.Text = "共 " + gdvUser.PageCount.ToString() + " 页";
            LblRecordCount.Text = "总共 " + ds.Tables[0].Rows.Count.ToString() + " 条";

            if (ds.Tables[0].Rows.Count == 0)
            {
                //如果当前记录条数为0 

                btnFirst.Visible = false;
                btnPrev.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;

                LblCurrentIndex.Visible = false;
                LblPageCount.Visible = false;
                LblRecordCount.Visible = false;

                LblNoRecord.Visible = true;
            }
            else if (gdvUser.PageCount == 1)
            {
                //否则不显示分页超链
                btnFirst.Visible = false;
                btnPrev.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;
            }
        }
        catch (Exception ex)
        {
            Response.Write("数据库错误，错误原因：" + ex.Message);
            Response.End();
        }
    }

    /// <summary>
    /// 翻页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        //翻页按钮
        string arg = ((LinkButton)sender).CommandArgument.ToString();
        switch (arg)
        {
            //上一页
            case "prev":
                if (gdvUser.PageIndex > 0)
                {
                    gdvUser.PageIndex -= 1;
                }
                break;

            //下一页
            case "next":
                if (gdvUser.PageIndex < (gdvUser.PageCount - 1))
                {
                    gdvUser.PageIndex += 1;
                }
                break;

            //最后一页
            case "last":
                gdvUser.PageIndex = (gdvUser.PageCount - 1);
                break;

            //首页
            default:
                gdvUser.PageIndex = 0;
                break;
        }

        this.GridviewBind(ds);
    }

    /// <summary>
    /// 绑定模板列控件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvUser_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        //获取当前gridview行
        GridViewRow gridRow = e.Row;
        //找到gridview中的模板列中的label控件 并取得相应的值 
        Label lblRoleID = (Label)gridRow.Cells[3].FindControl("lblRoleID");
        Label lblRoleName = (Label)gridRow.Cells[3].FindControl("lblRoleName");
        Label lblStatus = (Label)gridRow.Cells[4].FindControl("lblStatus");
        Label lblState = (Label)gridRow.Cells[4].FindControl("lblState");

        //根据角色ID的不同分别绑定相应的值
        if (lblRoleID != null)
        {
            if (lblRoleID.Text == "0")
            {
                lblRoleName.Text = "管理员";
            }
            if (lblRoleID.Text == "1")
            {
                lblRoleName.Text = "仓库管理员";
            }
            if (lblRoleID.Text == "2")
            {
                lblRoleName.Text = "业务员";
            }
        }
        //根据启用的状态不同分别绑定不同的值
        if (lblState != null)
        {
            if (lblStatus.Text == "True")
            {
                lblState.Text = "启用";
            }
            if (lblStatus.Text == "False")
            {
                lblState.Text = "禁用";
            }
        }
        e.Row.DataBind();
    }
  
}
